/****************************************************************************
 * Copyright (c) 2008, 2009 Andreas Unger and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andreas Unger - initial API and implementation 
 ****************************************************************************/

package org.eclipselabs.damos.codegen.c.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipselabs.damos.dml.FixedPointDataType;
import org.eclipselabs.damos.dml.Subsystem;
import org.eclipselabs.damos.execution.executionmodel.Node;
import org.eclipselabs.damos.expressions.util.FixedPointDataTypeUtil;

/**
 * @author Andreas Unger
 *
 */
public class NodeExtensionsHelper {

	public static String getName(Node node) {
		List<Subsystem> enclosingSubsystems = node.getEnclosingSubsystems();
		if (!enclosingSubsystems.isEmpty()) {
			enclosingSubsystems = new ArrayList<Subsystem>(enclosingSubsystems);
			Collections.reverse(enclosingSubsystems);
			StringBuilder sb = new StringBuilder();
			for (Subsystem subsystem : enclosingSubsystems) {
				sb.append(subsystem.getName());
				sb.append("_");
			}
			sb.append(node.getComponent().getName());
			return sb.toString();
		}
		return node.getComponent().getName();
	}
	
	public static String toString(FixedPointDataType type, Integer value) {
		return Long.toString(value << type.getFractionalLength());
	}
	
	public static String toString(FixedPointDataType type, String value) {
		try {
			return FixedPointDataTypeUtil.toFixedPoint(Double.parseDouble(value), type).toString();
		} catch (NumberFormatException e) {
			return "/* ERROR: " + e.getMessage() + " */ 0.0";
		}
	}

}
